<script setup lang="ts">
import { ref } from "vue";
import { useUserStore } from "@/stores/user";
import { usePlaylistAPI } from "@/services/playlist";
import { usePlaylistStore } from "@/stores/playlist";
import { ElMessage } from "element-plus";

const visible = defineModel("show");
const emit = defineEmits(["create"]);
const userStore = useUserStore();
const playlistAPI = usePlaylistAPI();
const playlistStore = usePlaylistStore();

const playlistName = ref("");
const playlistDesc = ref("");
const isPublic = ref(true);

const handleSubmit = async () => {
  if (playlistName.value.trim()) {
    try {
      const result = await playlistAPI.postCreatePlaylist(
        playlistName.value,
        playlistDesc.value,
        userStore.uid,
        isPublic.value
      );

      if (result) {
        ElMessage.success("歌单创建成功");
        playlistName.value = "";
        playlistDesc.value = "";
        visible.value = false;
      }
    } catch (error) {
      ElMessage.error("创建歌单失败，请重试");
    }
  }
};
</script>

<template>
  <el-dialog
    v-model="visible"
    title="创建歌单"
    width="400px"
    align-center
    destroy-on-close
  >
    <el-form>
      <el-form-item>
        <el-input
          v-model="playlistName"
          placeholder="请输入歌单名称"
          @keyup.enter="handleSubmit"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="playlistDesc"
          type="textarea"
          placeholder="请输入歌单描述"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-switch v-model="isPublic" active-text="公开" inactive-text="私密" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="flex justify-end">
        <el-button type="primary" @click="handleSubmit">创建</el-button>
      </div>
    </template>
  </el-dialog>
</template>
